package com.itheima.controller;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
@Api(tags = "检查组管理")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */

    @PostMapping("/add")
    @ApiOperation("新增检查组")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        log.info("新增检查组：{}",checkGroup);
        try {

            checkGroupService.add(checkGroup,checkitemIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 检查组分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("分页查询")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("检查组分页查询：{}",queryPageBean);
        return checkGroupService.findPage(queryPageBean);
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    @ApiOperation("删除检查组")
    public Result deleteById(@RequestParam Integer id){
        log.info("删除检查组：{}",id);
        try {
            checkGroupService.deleteById(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询检查组")
    public Result findById(Integer id){
        CheckGroup checkGroup = checkGroupService.findById(id);
        if(checkGroup != null){
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(checkGroup);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }


    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("编辑检查组")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        log.info("编辑检查组：{},{}",checkGroup,checkitemIds);
        log.info("编辑检查组id：{}",checkGroup.getId());
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有检查组")
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        if(checkGroupList != null && checkGroupList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(checkGroupList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    /**
     * 根据套餐id查询关联的所有检查组id
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    @ApiOperation("根据套餐id查询关联的检查组id")
    public Result findCheckGroupIdsBySetmealId(Integer setmealId){
        log.info("根据套餐id查询关联的检查组id：{}",setmealId);
        try{
            List<Integer> checkitemIds =
                    checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
